import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import { useUserStore } from "../stores/modules/user";

/**
 * @description 路由参数配置简介
 * @param path ==> 菜单路由
 * @param name ==> 路由 name
 * @param redirect ==> 路由重定向地址
 * @param component ==> 视图文件路径
 * @param meta ==> 路由菜单元信息
 * @param meta.icon ==> 菜单图标
 * @param meta.title ==> 路由标题
 * @param meta.isAffix ==> 菜单是否固定在标签页中 (首页通常是固定项)
 * @param meta.isKeepAlive ==> 当前路由是否缓存
 * */
const routeList = [
  {
    path: "/",
    redirect: '/chat'
  },
  {
    path: "/index",
    redirect: '/chat'
  },
  {
    path: "/chat",
    name: "chat",
    component: () => import("@/views/chatPage/index.vue"),
    meta: {
      title: "华夏金租智能问答系统"
    }
  },
]
const router = createRouter({
  history: createWebHistory(),
  routes: routeList,
  strict: false,
  scrollBehavior: () => ({ left: 0, top: 0 })
});
/**
 * @description 路由拦截 beforeEach
 * */
router.beforeEach(async (to, from, next) => {
  // const userStore = useUserStore();
  // userStore.setUserInfo({
  //   name: '王多多',
  //   mobile: '17600186609',
  //   age: '20'
  // })
  //  // 1.判断是否携带登录信息，携带登录信息进行登录认证换取token，否则跳转回单点登录系统登录页面
  // if(to.fullPath.includes('?tokens=')) {
  //   const token = to.fullPath.split('=')[1]
  //   userStore.setToken(token)
  //   console.log(userStore.token)
  //   console.log(userStore.userInfo)
  // }  
  // // 2.动态设置标题
  // document.title = to.meta.title as string;
  // const userData = JSON.parse(localStorage.getItem('user') || "{}")
  // if (userData.token || userStore.token)  {
  //   return next(from.fullPath);
  // }
  // if (!userData.token && !userStore.token) return alert('跳转回登录页面！')
  next();
});

/**
 * @description 路由跳转错误
 * */
router.onError(error => {

  console.warn("路由错误", error.message);
});

/**
 * @description 路由跳转结束
 * */
router.afterEach(() => {

});

export default router;